"""
@author: ydh
@file: 三乐购物系统.py
@time: 2023/3/28 20:50
"""
import json
import sys


uinfo= {}
price= {}
def enter(username,password):
   fp =open("uinfo.txt")
   u1_str=fp.read()
   uinfo=json.loads(u1_str)
   terminal=int(input("请输入购物预算"))

   if username in uinfo.keys() and password == uinfo[username]['password']:
       print(f"欢迎{username}用户登录三乐购物系统")
   else:
        uinfo[username]={"password":password}
        u_str=json.dumps(uinfo)
        fp=open("uinfo.txt","w")
        fp.write(u_str)
        fp.close()
   print("请输入你的选择:1.查找商品信息、购买商品 2.添加商品信息 3.结算 4.查看已购买的商品 5.按exit退出本次操作")
   buyed= {}
   sum=0
   while 1:
        fp = open("priceinfo.txt")
        price_str = fp.read()
        price = json.loads(price_str)
        choice = input("请输入你的选择")
        if choice == 'exit':
            print(f"已购买的商品有:{buyed}")
            print(f"余额为{terminal-sum}")
            break
        elif choice == '1':
            choice1=input('请输入你要查找的商品编号')
            if choice1 in price.keys():
                print(f"{choice1}编号的商品信息是{price[choice1]}")
                buy_choice=input("请问是否选择加入购物车:是或否")
                if buy_choice == "是":
                    num1,liang=input("请输你的需要购买的商品编号、数量").split()
                    buyed[num1]=price[num1]
                    buyed[num1]['liang']=liang
                    sum=sum+int(buyed[num1]["momney"])*int(liang)
                else:
                    continue
            else:
                print("你搜索的商品不存在")
        elif choice == '2':

            number, name, momney = input("请输入商品编号、商品名字、价格（空白分割）").split()
            price[number] = {"name": name, "momney": momney}
            price_str=json.dumps(price)
            fp =open("priceinfo.txt","w")
            fp.write(price_str)
            fp.close()
        elif choice == '3':
            if terminal>sum:
                print(f"购买成功,还剩{terminal-sum}")
            else:
                print("超出预算了")
        elif choice == '4':
            print(buyed)
        else:
            print('输入不合法')



username=input("请输入你的用户名")
password=input("请输入你的密码")
enter(username,password)
